package com.example.flag03;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyCursorAdapter extends CursorAdapter {
    private String fromTime;
    private String toTime;
    private String titleText;
    private String signal;
    private int _id;
//    用于判断是否过期
    private int nowYear;
    private int nowMonth;
    private int nowDay;
    private int endYear;
    private int endMonth;
    private int endDay;

    //    数据库变量
    private MyDatabaseHelper databaseHelper;

    public MyCursorAdapter(Context context, Cursor cursor, int nowYear,int nowMonth,int nowDay, MyDatabaseHelper databaseHelper) {
        super(context, cursor);
        this.nowYear=nowYear;
        this.nowMonth=nowMonth;
        this.nowDay=nowDay;
        this.databaseHelper=databaseHelper;
    }


    @Override
    public void changeCursor (Cursor cursor) {
        Cursor old = swapCursor(cursor);
        if (old != null) {
            old.close();
        }
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

////        布局样式
//        View view=View.inflate(context,R.layout.layout,null);
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
        View view=inflater.inflate(R.layout.layout,parent,false);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        fromTime=cursor.getString(cursor.getColumnIndex("fromYear"))+"年"+
                cursor.getString(cursor.getColumnIndex("fromMonth"))+"月"+
                cursor.getString(cursor.getColumnIndex("fromDay"))+"日"+"~";
        TextView textView1=(TextView)view.findViewById(R.id.fromTime);
        textView1.setText(fromTime);

        toTime=cursor.getString(cursor.getColumnIndex("toYear"))+"年"+
                cursor.getString(cursor.getColumnIndex("toMonth"))+"月"+
                cursor.getString(cursor.getColumnIndex("toDay"))+"日";
        TextView textView2=(TextView)view.findViewById(R.id.toTime);
        textView2.setText(toTime);

        titleText=cursor.getString(cursor.getColumnIndex("flagTitle"));
        TextView textView3=(TextView)view.findViewById(R.id.titleText);
        textView3.setText(titleText);

        _id=cursor.getInt(cursor.getColumnIndex("_id"));
        signal=cursor.getString(cursor.getColumnIndex("signal"));

//        设置文本粗体,汉字不能直接设置
        TextView tv1 = (TextView)view.findViewById(R.id.timeTitle);
        TextPaint tp1 = tv1.getPaint();
        tp1.setFakeBoldText(true);

        TextView tv2 = (TextView)view.findViewById(R.id.titleFlag);
        TextPaint tp2 = tv2.getPaint();
        tp2.setFakeBoldText(true);

//      查询有多少个过期，设置为过期状态
        endYear=cursor.getInt(cursor.getColumnIndex("toYear"));
        endMonth=cursor.getInt(cursor.getColumnIndex("toMonth"));
        endDay=cursor.getInt(cursor.getColumnIndex("toDay"));

        //        设置图片
        String nowTime=""+nowYear+nowMonth+nowDay;
        String endTime=""+endYear+endMonth+endDay;
        if(nowTime.compareTo(endTime)>0&&signal!="-1"){
            ImageView imageView = (ImageView) view.findViewById(R.id.signalImage);
            imageView.setImageResource(R.drawable.out);
//            设置为过期状态
            signal="-1";
            setSignal();
        }else {
            signal = cursor.getString(cursor.getColumnIndex("signal"));
            ImageView imageView = (ImageView) view.findViewById(R.id.signalImage);
            switch (signal) {
                case "-1":
                    imageView.setImageResource(R.drawable.out);
                    break;
                case "0":
                    imageView.setImageResource(R.drawable.wait);
                    break;
                case "1":
                    imageView.setImageResource(R.drawable.correct);
                    break;
            }
        }
    }

//      设置为过期状态
    private void setSignal() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("signal", signal);
        values.put("finishedYear",nowYear);
        values.put("finishedMonth",nowMonth);
        values.put("finishedDay",nowDay);
        db.update("Flag",values,"_id="+_id,null);
    }

}
